package ifmo.group5512.timer;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * User: katerina
 * Date: 01.11.12
 * Time: 0:03
 * To change this template use File | Settings | File Templates.
 */
public class InteractionProcessor implements Runnable {

    private ServerSocket serverSocket;

    public boolean init() {
        if (isInitialized()) {
            return true;
        }

        try {
            serverSocket = new ServerSocket(18080);
        } catch (IOException ex) {
            return false;
        }

        return true;
    }

    public boolean isInitialized() {
        return serverSocket != null;
    }

    private void processIteration() {
        Socket socket = null;
        try {
            socket = serverSocket.accept();
        } catch (IOException ex) {
            System.err.println("Unable to accept client socket.");
            return;
        }

        new Thread(new Communicator(socket)).start();
    }

    @Override
    public void run() {
        while (true) {
            processIteration();
        }
    }
}
